﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using System.Text;

namespace Netduino.NetBios
{
    /// <summary>
    /// Test code for Neduino Netbios reading messages as per http://netmf.codeplex.com/workitem/1166
    /// </summary>
    public class Program2
    {

        // public static void Main()
        //{
        //           //allow the use of DHCP for the Network interface
        //    Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].EnableDhcp();

        //     //WIZnet_W5100.Enable(SPI.SPI_module.SPI1, (Cpu.Pin)FEZ_Pin.Digital.Di10, (Cpu.Pin)FEZ_Pin.Digital.Di7, true);
        //    //Dhcp.EnableDhcp(new byte[] { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA},"FEZ");
        //    NetBios2 MyNetBios=new NetBios2("FEZ");
 
        //    Debug.Print("Now you can ping FEZ !");
        //    Thread.Sleep(Timeout.Infinite);
        //}

        //public static void Main()
        //{
        //    // While this is running on the NetduinoPlus, navigate to http://netduino (on a machine on the same subnet)

        //    using (var serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
        //    {
        //        EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 137);
        //        serverSocket.Bind(remoteEndPoint);

        //        while (true)
        //        {
        //            if (serverSocket.Poll(-1, SelectMode.SelectRead))
        //            {
        //                var inBuffer = new byte[serverSocket.Available];
        //                int count = serverSocket.ReceiveFrom(inBuffer, ref remoteEndPoint);

        //                if (count > 0) Debug.Print("SUCCESS! We see a Netbios name request! ");
        //            }
        //        }
        //    }
        //}

        //public static void Main()
        //{
        //    // Open Wireshark, then run this to see if the message "TEST" is broadcast

        //    using (var clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
        //    {
        //        Debug.Print("SenDING UDP packet...");

        //        IPHostEntry entry = Dns.GetHostEntry("192.168.1.255"); // This the address to local broadcast for me, may be different for others.
        //        IPAddress ipAddress = entry.AddressList[0];
        //        IPEndPoint brodcastEndPoint = new IPEndPoint(ipAddress, 137);

        //        clientSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true); // Enable broadcast

        //        clientSocket.SendTo(Encoding.UTF8.GetBytes("TEST"), brodcastEndPoint);
        //        // This actually generates a Malformed Netbios response from the router, but it does demonstrate UDP Broadcast.

        //        Debug.Print("DONE! Sent UDP packet! ");
        //    }
        //}
    }
}
